﻿
@{
	Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<input type="hidden" name="id" id="id" class="layui-input" />

<div class="layui-form-item">
	<label class="layui-form-label">工作站名称</label>
	<div class="layui-input-block">
		<input type="text" id="workstationName" name="workstationName" lay-verify="required" style="width:350px" placeholder="请输入" class="layui-input">
	</div>
</div>


<div class="layui-form-item">
	<label class="layui-form-label">工作站地点</label>
	<div class="layui-input-block">
		<input type="text" id="workstationPlace" name="workstationPlace" placeholder="请输入" lay-verify="required" style="width:350px" class="layui-input">
	</div>
</div>
<div class="layui-form-item">
	<label class="layui-form-label">所在车间</label>
	<div class="layui-input-block" style="width:350px">
		<select id="workshopName" name="workshopName" style="width:350px" lay-filter="workshopName">
			<option value="">请选择</option>
			<option value="五金车间">五金车间</option>
			<option value="组装车间">组装车间</option>
			<option value="注塑车间">注塑车间</option>
			<option value="二楼插丝车间">二楼插丝车间</option>
			<option value="二楼焊接车间">二楼焊接车间</option>
		</select>
	</div>
</div>
<div class="layui-form-item">
	<label class="layui-form-label">所属工序</label>
	<div class="layui-input-block" style="width:350px">
		<select id="processBelongs" name="processBelongs" style="width:350px" lay-filter="processBelongs">
			<option value="">请选择</option>
			<option value="板材剪贴">板材剪贴</option>
			<option value="组装">组装</option>
			<option value="QCC质检">QCC质检</option>
			<option value="包装">包装</option>
			<option value="尼龙棒">尼龙棒</option>
		</select>
	</div>
</div>
<div class="layui-form-item">
	<label class="layui-form-label">是否启用</label>
	<div class="layui-input-block">
		<input type="radio" name="state" value=1 title="启用">
		<input type="radio" name="state" value=0 title="禁用">
	</div>
</div>

<div class="layui-form-item">
	<div class="layui-input-block">
		<button type="submit" class="layui-btn" lay-submit lay-filter="updateWorkstation" id="update" onclick="update()">保存</button>
		<button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">关闭</button>
	</div>
</div>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>

		$(function(){
			findWorkstation();
		});

		function findWorkstation(){
			$.ajax({
			   url: '/WorkStation/FindWorkstationById',
				data:{id:@ViewBag.id},
				type:'get',
				datatype:'json',
				async:false,
				success:function(res){
					console.log(res)
					$("#id").val(res.id);
					$("#workstationCode").val(res.workstationCode);
					$("#workstationName").val(res.workstationName);
					$("#workstationPlace").val(res.workstationPlace);
					$("[name='workshopName']").val(res.workshopName);
					$("#processBelongs").val(res.processBelongs);
					$("[name='state'][value=" + res.state + "]").prop("checked", true);
					layui.form.render("radio");
					layui.form.render("select");
				}
			})
		}

		 function update(){
				var data={
					id: @ViewBag.id,
				//	id: $("#id").val(),
					 workstationCode:$("#workstationCode").val(),
					 workshopName:$("#workshopName").val(),
					 workstationPlace:$("#workstationPlace").val(),
					 processBelongs:$("#processBelongs").val(),
					 state:$("[name='state']").val()
				}

				$.ajax({
					url:'/Workstation/EditWorkstation',
					data:data,
					type:'post',
					dataType:'json',
						async:false,
					success:function(res){
						alert(res)
						if(res>0){
							layer.alert('修改成功', { title: '操作结果' }, function () {
								// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
								var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
								window.parent.location.reload(); // 刷新父页
								parent.layer.close(index); //再执行关闭
								 setTimeout(function () { parent.layer.close(index) }, 50000);//延迟s
							})
						}else{
							layer.alert('工作站名称在表中已存在');
							return;
						}
					},
					error:function (res) {

					}
				})
		  }

		//关闭弹框
			function closeWorkstation() {
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.location.reload(); // 刷新父页
				parent.layer.close(index); //再执行关闭
			}

		   layui.use(['form', 'laydate', 'util'], function () {
				var form=layui.form;
				var layer=layui.layer;
				var laydate=layui.laydate;
				var util=layui.util;
			//指定开关事件，生成工作站编码
			form.on('switch(switchTest)',function(data){
				if(data.elem.checked){
					var random =Math.floor(Math.random()*10)+'';
					for(var i=0;i<3;i++){
						random+=Math.floor(Math.random()*10);
					}
					random='WS'+random;
					$('#workstationCode').val(random);
				}else{
					$('#workstationCode').val("");
				}
			});
			});


		// layui.use(['form'],function(){
		// 	var form=layui.form;
			//$("#update").on('click',)

			// form.on('submit(updateWorkstation)',function(data){
			// var field=data.field;
			// field.id= parseInt(field.id);
			// 	$.ajax({
			// 		url:'/Workstation/EditWorkstation',
			// 		data:field,
			// 		type:'post',
			// 		dataType:'json',
			// 		success:function(res){
			// 			alert(res)
			// 			if(res>0){
			// 				layer.alert('修改成功', { title: '操作结果' }, function () {
			// 					// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
			// 					var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
			// 					window.parent.location.reload(); // 刷新父页
			// 					parent.layer.close(index); //再执行关闭
			// 					 setTimeout(function () { parent.layer.close(index) }, 50000);//延迟s
			// 				})
			// 			}else{
			// 				layer.alert('工作站名称在表中已存在');
			// 				return;
			// 			}
			// 		},
			// 		error:function (res) {

			// 		}
			// 	})
			// })

	//	})

</script>